package com.health.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.common.entity.CheckGroup;
import com.health.common.entity.CheckItem;
import com.health.common.entity.CheckItemWithGroup;
import com.health.common.vo.QueryPageBean;
import com.health.common.vo.RespBean;
import com.health.common.vo.RespPageBean;
import com.health.mapper.CheckGroupMapper;
import com.health.mapper.CheckItemWithGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemWithGroupMapper checkItemWithGroupMapper;

    public List<CheckGroup> getAllCheckGroup() {
        return checkGroupMapper.getAllCheckGroup();
    }

    public RespPageBean getAllCheckGroupByCondition(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        if(pageSize == null){
            pageSize = 5;
        }
        if(currentPage == null || currentPage == 1){
            currentPage = 0;
        }else{
            currentPage = (currentPage-1)*pageSize;
        }

        System.out.println("currentPage="+currentPage+",pageSize="+pageSize+",queryString="+queryString);
        List<CheckGroup> data = checkGroupMapper.getAllCheckGroupByCondition(currentPage,pageSize,queryString);
        Long total = checkGroupMapper.getTotalByQueryString(queryString);

        RespPageBean pageBean = new RespPageBean();
        pageBean.setData(data);
        pageBean.setTotal(total);
        return pageBean;
    }

    @Transactional
    public Integer addCheckGroup(CheckGroup checkGroup, Integer[] items) {
        int insert = checkGroupMapper.insert(checkGroup);
        Integer groupId4DB = checkGroup.getId();
        CheckItemWithGroup checkItemWithGroup = new CheckItemWithGroup();
        checkItemWithGroup.setCheckGroupId(groupId4DB);
        for (Integer item : items) {
            checkItemWithGroup.setCheckItemId(item);
            checkItemWithGroupMapper.insert(checkItemWithGroup);
        }
        return insert;
    }

    @Transactional
    public Integer deleteCheckGroupById(Integer id) {
        int i = checkGroupMapper.deleteById(id);
        QueryWrapper<CheckItemWithGroup> w = new QueryWrapper<>();
        w.eq("checkgroup_id",id);
        checkItemWithGroupMapper.delete(w);
        return i;
    }

    @Transactional
    public Integer updateCheckGroup(CheckGroup checkGroup, Integer[] items) {
        System.out.println("checkGroup="+checkGroup);
        Integer checkGroupId = checkGroup.getId();
        int i = checkGroupMapper.updateById(checkGroup);
        if(items.length>0){
            QueryWrapper<CheckItemWithGroup> w = new QueryWrapper<>();
            w.eq("checkgroup_id",checkGroupId);
            checkItemWithGroupMapper.delete(w);
            CheckItemWithGroup checkItemWithGroup = new CheckItemWithGroup();
            checkItemWithGroup.setCheckGroupId(checkGroupId);
            for (Integer item : items) {
                checkItemWithGroup.setCheckItemId(item);
                checkItemWithGroupMapper.insert(checkItemWithGroup);
            }
        }
        return i;
    }

    public List<CheckItem> getCheckItemByGroupId(Integer id) {
        return checkGroupMapper.getCheckItemByGroupId(id);
    }

    public Integer[] getCheckItemIdsByGroupId(Integer id) {
        return checkGroupMapper.getCheckItemIdsByGroupId(id);
    }

    public List<CheckGroup> getGroup4SetMeal(Integer id) {
        return checkGroupMapper.getGroup4SetMeal(id);
    }

    public List<CheckGroup> getAllCheckGroup4SetMeal() {
        return checkGroupMapper.selectList(null);
    }
}
